import { Audio } from "@/utils/Audio.js";
import { BgmAudio } from "@/utils/Audio/BgmAudio.js";
import { BgsAudio } from "@/utils/Audio/BgsAudio.js";
import { SeAudio } from "@/utils/Audio/SeAudio.js";
import { SeStaticAudio } from "@/utils/Audio/SeStaticAudio";
import { sound } from "@pixi/sound";

export class AudioManager {
    constructor() {
        throw new Error('This is a static class');
    }

    static set masterVolume(value){
        if(value!=this.volume){
            this.volume=value;
            this.bgm.volume=this.bgm._volume;
            this.bgs.volume=this.bgs._volume;
            this.me.volume=this.me._volume;
            this.se.volume=this.se._volume;
            this.staticSe.volume=this.staticSe._volume;
        }
    }

    static get masterVolume(){
        return this.volume??1;
    }

    static get bgmVolume() {
        return this.bgm.volume*100;
    }

    static set bgmVolume(value) {
        this.bgm.volume=value/100;
    }

    static get bgsVolume() {
        return this.bgs.volume*100;
    }

    static set bgsVolume(value) {
        this.bgs.volume=value/100;
    }

    static get meVolume() {
        return this.me.volume*100;
    }

    static set meVolume(value) {
        this.me.volume=value/100;
    }

    static get seVolume() {
        return this.se.volume*100;
    }

    static set seVolume(value) {
        this.se.volume=value/100;
        this.staticSe.volume=value/100;
    }


    static init(){
        this.volume = 1;   // (min: 0, max: 1)
        this.replayFadeTime = 1;
        this.bgm = new BgmAudio(this);
        this.bgs = new BgsAudio(this);
        this.me = new Audio('me',this);
        this.se = new SeAudio(this);
        this.staticSe=new SeStaticAudio(this);
    }

    static destroy(){
        this.bgm.clear();
        this.bgs.clear();
        this.me.clear();
        this.se.clear();
        this.staticSe.clear();
        sound.close();
    }

    /**
     * 
     * @param {{name,pan,pitch,volume}} bgm 
     * @param {} pos 
     */
    static playBgm(bgm) {
       this.bgm.play(bgm.name);
    }

    static replayBgm(bgm) {
        this.bgm.play(bgm.name);
        this.bgm.fadeIn(this.replayFadeTime);
    }

    static stopBgm() {
        this.bgm.pause();
    }

    static fadeOutBgm(duration) {
        this.bgm.fadeOut(duration??this.replayFadeTime);
    }

    static fadeInBgm(duration) {
        this.bgm.fadeIn(duration??this.replayFadeTime);
    }

    static playBgs(bgs) {
        this.bgs.play(bgs.name);
    }

    static replayBgs(bgs) {
        this.bgs.play(bgs.name);
        this.bgs.fadeIn(this.replayFadeTime);
    }

    static stopBgs() {
        this.bgs.pause();
    }

    static fadeOutBgs(duration) {
        this.bgs.fadeOut(duration??this.replayFadeTime);
    }

    static fadeInBgs(duration) {
        this.bgs.fadeIn(duration??this.replayFadeTime);
    }

    static playMe(me) {
        this.me.play(me.name);
    }

    static fadeOutMe(duration) {
        this.me.fadeOut(duration??this.replayFadeTime);
    }

    static stopMe() {
        this.me.pause();
    }

    static playSe(se) {
        this.se.play(se.name);
    }

    static stopSe() {
        this.se.stop();
    }

    static playStaticSe(se) {
        this.staticSe.play(se.name);
    }

    static loadStaticSe(se) {
        this.staticSe.load(se.name);
    }


    static stopAll() {
        this.stopMe();
        this.stopBgm();
        this.stopBgs();
        this.stopSe();
    }

    static saveBgm() {
        return {
            name: this.bgm.name,
            volume: this.bgm.volume,
            pitch: this.bgm.speed*100,
            pan: this.bgm.pan*100,
            pos: 0
        };
    }

    static saveBgs() {
        return {
            name: this.bgs.name,
            volume: this.bgs.volume,
            pitch: this.bgs.speed*100,
            pan: this.bgs.pan*100,
            pos: 0
        };
    }
}


